package com.gitee.wsl.io.buffer

/**
 * Convenience method, forces a Byte to an Int while stripping all sign bits.  the Byte becomes
 * the LSB of the Int produced.  For example, byte of 0xFF becomes 0x000000FF or 255 as the result Int.
 */
internal infix fun ByteArray.toPosInt(index: Int): Int {
    return this[index].toInt() and 0xFF
}

/**
 * Convenience method, forces a Byte to an UInt while stripping all sign bits.  the Byte becomes
 * the LSB of the Int produced.  For example, byte of 0xFF becomes 0x000000FF or 255 as the result Int.
 */
internal infix fun ByteArray.toPosUInt(index: Int): UInt {
    return this[index].toUInt() and 0xFFu
}

/**
 * Convenience method, forces a UByte to an Int while stripping all sign bits.  the Byte becomes
 * the LSB of the Int produced.  For example, byte of 0xFF becomes 0x000000FF or 255 as the result Int.
 */
@OptIn(ExperimentalUnsignedTypes::class)
internal infix fun UByteArray.toPosInt(index: Int): Int {
    return this[index].toInt() and 0xFF
}

/**
 * Convenience method, forces a Byte to an UInt while stripping all sign bits.  the Byte becomes
 * the LSB of the Int produced.  For example, byte of 0xFF becomes 0x000000FF or 255 as the result Int.
 */
@OptIn(ExperimentalUnsignedTypes::class)
internal infix fun UByteArray.toPosUInt(index: Int): UInt {
    return this[index].toUInt() and 0xFFu
}

/**
 * Convenience method, forces a Byte to a Long while stripping all sign bits.  the Byte becomes
 * the LSB of the Long produced.  For example, byte of 0xFF becomes 0x00000000000000FF or 255 as
 * the result Long.
 */
internal infix fun ByteArray.toPosLong(index: Int): Long {
    return this[index].toLong() and 0xFF
}

/**
 * Convenience method, forces a Byte to a ULong while stripping all sign bits.  the Byte becomes
 * the LSB of the Long produced.  For example, byte of 0xFF becomes 0x00000000000000FF or 255 as
 * the result Long.
 */
internal infix fun ByteArray.toPosULong(index: Int): ULong {
    return this[index].toULong() and 0xFFu
}

/**
 * Convenience method, forces a UByte to a Long while stripping all sign bits.  the Byte becomes
 * the LSB of the Long produced.  For example, byte of 0xFF becomes 0x00000000000000FF or 255 as
 * the result Int.
 */
@OptIn(ExperimentalUnsignedTypes::class)
internal infix fun UByteArray.toPosLong(index: Int): Long {
    return this[index].toLong() and 0xFF
}

/**
 * Convenience method, forces a UByte to a ULong while using only the LSB.
 * For example, byte of 0xFF becomes 0x00000000000000FF or 255 as the result ULong.
 */
@OptIn(ExperimentalUnsignedTypes::class)
internal infix fun UByteArray.toPosULong(index: Int): ULong {
    return this[index].toULong() and 0xFFu
}